package com.shu.timerTask;

import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2019/4/8 10:45
 */

public class ThreadTask {

    /**
     * 停止一个线程 + 延时1秒钟执行一个任务
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void name() throws IOException, InterruptedException {
        CountDownLatch downLatch = new CountDownLatch(1);
        Runnable runnable = () -> {
            while (true) {
                System.out.println("..." + Thread.currentThread().isInterrupted());
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程停止");
                    downLatch.countDown();
                    return;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("任务执行完毕...准备停止线程");
                    Thread.currentThread().interrupt();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(runnable).start();
        downLatch.await();
    }


}
